/**
 * jims
 */
package com.jims.master.modules.nurse.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jims.master.common.utils.DateUtils;
import com.jims.master.modules.doctor.entity.PatVisit;
import com.jims.master.modules.doctor.service.PatVisitService;
import com.jims.master.modules.finance.entity.InpBillDetail;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.nurse.entity.NurseReturnFeeApply;
import com.jims.master.modules.nurse.service.NurseReturnFeeApplyService;

import java.util.Date;
import java.util.List;

/**
 * 护士退费申请Controller
 *
 * @author liguoxian
 * @version 2017-07-27
 */
@Controller
@RequestMapping(value = "${adminPath}/nurse/nurseReturnFeeApply")
public class NurseReturnFeeApplyController extends BaseController {

    @Autowired
    private NurseReturnFeeApplyService nurseReturnFeeApplyService;
    @Autowired
    private PatVisitService patVisitService;

    @RequiresPermissions("user")
    @RequestMapping(value = {"index", ""})
    public String list(NurseReturnFeeApply nurseReturnFeeApply, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<NurseReturnFeeApply> page = nurseReturnFeeApplyService.findPage(new Page<NurseReturnFeeApply>(request, response), nurseReturnFeeApply);
        model.addAttribute("page", page);
        return "modules/nurse/nurseReturnFeeApplyIndex";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "saveReturnFee")
    @ResponseBody
    public StringData saveReturnFee(@RequestBody List<NurseReturnFeeApply> nurseReturnFeeApplys) {
        StringData data = new StringData();
        int i = nurseReturnFeeApplyService.saveBatch(nurseReturnFeeApplys);
        if (i > 0) {
            data.setCode("success");
            data.setData("保存退费申请成功");
        } else {
            data.setCode("error");
            data.setData("保存退费申请失败");
        }
        return data;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "delete")
    @ResponseBody
    public StringData delete(NurseReturnFeeApply nurseReturnFeeApply) {
        StringData data = new StringData();
        int i = nurseReturnFeeApplyService.delete(nurseReturnFeeApply);
        if (i > 0) {
            data.setCode("success");
            data.setData("取消成功");
        } else {
            data.setCode("error");
            data.setData("取消失败");
        }
        return data;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "confirm")
    @ResponseBody
    public StringData confirm(String id) {
        StringData data = new StringData();
        int i = nurseReturnFeeApplyService.confirm(id);
        if (i > 0) {
            data.setCode("success");
            data.setData("确认成功");
        } else {
            data.setCode("error");
            data.setData("确认失败");
        }
        return data;
    }

    /**
     * 护士退费数据查询
     *
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "returnFee")
    public String returnFee(PatVisit patVisit, String itemName, Integer type, Model model) {
        InpBillDetail inpBillDetail = new InpBillDetail();
        inpBillDetail.setPatientId(patVisit.getPatientId());
        inpBillDetail.setVisitId(patVisit.getId());
        inpBillDetail.setItemName(itemName);
        String date = patVisit.getStartDate();
        if (StringUtils.isBlank(date)) {
            date = DateUtils.getDate();
        }
        inpBillDetail.setStartDate(date + " 00:00:00");
        inpBillDetail.setEndDate(date + " 23:59:59");
        inpBillDetail.setOrgId(UserUtils.getOrgId());
        model.addAttribute("patVisit", patVisitService.get(patVisit));
        model.addAttribute("itemName", itemName);
        model.addAttribute("date", date);
        if (type == null) type = 0;
        // 0 未退费， 1 退费
        if (type == 0) {
            model.addAttribute("list", nurseReturnFeeApplyService.findNotReturnList(inpBillDetail));
        } else {
            if (type == 2) {
                inpBillDetail.setUpdateDate(new Date());
            }
            model.addAttribute("list", nurseReturnFeeApplyService.findReturnList(inpBillDetail));
        }

        model.addAttribute("type", type);
        return "modules/nurse/nurseReturnFee";
    }

    /**
     * 病人退费待确认数据
     *
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "notConfirmReturn")
    public String findNotConfirmReturnList(InpBillDetail inpBillDetail, Model model) {
        inpBillDetail.setOrgId(UserUtils.getOrgId());
        inpBillDetail.setNurseReturnFeeApply(new NurseReturnFeeApply());
        inpBillDetail.getNurseReturnFeeApply().setApplyDept(UserUtils.getUser().getOrgDept().getId());
        model.addAttribute("name", inpBillDetail.getName());
        model.addAttribute("itemName", inpBillDetail.getItemName());
        model.addAttribute("list", nurseReturnFeeApplyService.findNotConfirmReturnList(inpBillDetail));
        return "modules/nurse/nurseReturnFeeConfirm";
    }
}